package com.graduation.sports.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduation.sports.R;
import com.graduation.sports.base.BaseActivity;
import com.graduation.sports.bean.UserInfo;
import com.lxj.xpopup.XPopup;

/**
 * Created by apc_allen
 * date : 2/16/21
 * description :
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private String TITLE_NAME = "注册";
    private View title_back;
    private TextView titleText;

    private Context mContext;
    private EditText et_account;
    private EditText et_username;
    private EditText et_password;
    private EditText et_repassword;
    private EditText et_height;
    private EditText et_weight;
    private TextView tvGrade;
    private Button register_login;
    private RadioGroup radio_sex;

    private int grade = 0;

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initDataAndEvent(Bundle savedInstanceState) {
        initView();
    }

    private void initView(){

        title_back = findView(R.id.title_back);
        titleText = findView(R.id.titleText);

        et_account = findView(R.id.reg_et_account);
        et_username = findView(R.id.reg_et_username);
        et_password = findView(R.id.reg_et_password);
        et_repassword = findView(R.id.reg_et_repassword);
        tvGrade = findView(R.id.reg_et_grade);
        et_height = findView(R.id.reg_et_height);
        et_weight = findView(R.id.reg_et_weight);

        radio_sex = findView(R.id.radio_sex);
        register_login = findView(R.id.reg_btn_register);

        titleText.setText(TITLE_NAME);

        title_back.setOnClickListener(this);
        tvGrade.setOnClickListener(this);
        register_login.setOnClickListener(this);
    }

    private void insertUser(){
        String userAccount = et_account.getText().toString().trim();
        String userName = et_username.getText().toString().trim();
        String userPwd = et_password.getText().toString().trim();
        String rePwd = et_repassword.getText().toString().trim();
        String sex = "女";
        if (radio_sex.getCheckedRadioButtonId() == R.id.reg_rd_male) {
            sex = "男";
        }
        String height = et_height.getText().toString().trim();
        String weight = et_weight.getText().toString().trim();

        if (TextUtils.isEmpty(userAccount)) {
            ToastUtils.showShort("请输入帐号！");
            return;
        }
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showShort("请输入用户名！");
            return;
        }
        if (TextUtils.isEmpty(userPwd)) {
            ToastUtils.showShort("请输入密码！");
            return;
        }

        // 判断两次密码
        if (!userPwd.equals(rePwd)) {
            ToastUtils.showShort("两次密码输入不一致");
            return;
        }

        if (grade < 1) {
            ToastUtils.showShort("请选择年级");
            return;
        }

        showLoadingDialog("注册中......");
        UserInfo userInfo = new UserInfo();
        userInfo.setUserAccount(userAccount);
        userInfo.setUserName(userName);
        userInfo.setPassword(userPwd);
        userInfo.setGender(sex);
        userInfo.setHeight(height);
        userInfo.setWeight(weight);
        userInfo.setGrade(grade);

        if (userInfo.save()) {
            closeLoadingDialog();
            SPUtils.getInstance().put("account",userAccount);
            SPUtils.getInstance().put("password",userPwd);
//            openActivity(SportsLoginActivity.class);
            startActivity(new Intent(RegisterActivity.this, SportsLoginActivity.class));

            finish();
        } else {
            closeLoadingDialog();
            ToastUtils.showShort("注册失败！");
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back: {
                finish();
            }
            break;
            case R.id.reg_btn_register:
                insertUser();
                break;
            case R.id.reg_et_grade:
                showGradeChoose();
                break;
        }
    }

    private void showGradeChoose(){
        new XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .asCenterList("选择年级", getResources().getStringArray(R.array.gradeNames),
                        (position, text) -> {
                            grade = position + 1;
                            tvGrade.setText(text);
                        }).show();

    }
}
